package com.dom4j;

import java.io.File;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.util.ConnUtil;


/** 
 * @author 作者：zhaosj 
 * @version 创建时间：Dec 16, 2009 3:35:52 PM 
 * 类说明 
 */

public class XmlOperate {
	
	public static void main(String [] args)
	{
		Connection newCon 	= null ;
		Statement newst 	= null ;
		Statement newst2 	= null ;
		ResultSet newrs 	= null ;
		ResultSet newrs2 	= null ;
		
		try {
			//建立数据库连接
			newCon = ConnUtil.getConnection("localOracle") ;
			newst  = newCon.createStatement() ;
			newst2 = newCon.createStatement() ;
			
			// 声明写XML的对象
        	XMLWriter writer = null;

        	// 设置XML文件的编码格式
        	OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("gbk");
            format.setLineSeparator("\r\n") ;

            // 设定磁盘文件信息
            String filePath = ".\\phonetype.xml" ;
            File file = new File(filePath) ;
            
            // 定义文档对象
            Document _document = DocumentHelper.createDocument();
            Element _root = _document.addElement("brandtypeconfig");
            
            String mobileBrand = "" ;
    		String mobileName  = "" ;
    		String mobileModel = "" ;
    		long mobileId = -1L ;
            
            String tmpSql = "select distinct (m.mobile_brand), d.key_name "
            				+ " from mobile_info m, dictionary d "
            				+ " where m.mobile_brand = d.key_code "
            				+ " order by 1 " ;
			newrs = newst.executeQuery(tmpSql);
			int brandId = 1 ;
			while(newrs.next())
			{
				mobileBrand = newrs.getString(1) ;
				mobileName  = newrs.getString(2) ;
				
				String maxDateSql = "select m.mobile_modle,m.mobile_id " 
									+ " from mobile_info m "
									+ " where m.mobile_brand = '" + mobileBrand + "'"
									+ " order by m.mobile_modle" ;
				
				newrs2 = newst2.executeQuery(maxDateSql);

				StringBuffer sb = new StringBuffer() ;
				while (newrs2.next()) {
					
					mobileModel = newrs2.getString(1) ;
					mobileId	= newrs2.getLong(2) ;
					
					sb.append(mobileModel + "," + mobileId + ";") ;
				}
				
				String str = sb.toString() ;
				if (str.endsWith(";"))
				{
					str = str.substring(0 , str.length() - 1) ;
				}
				
				//System.out.println(mobileBrand+":"+mobileName);
				//System.out.println(str+"\n");
				
				//添加标签及属性
                Element _brand = _root.addElement("brand");
                _brand.addAttribute("brandid", String.valueOf(brandId)) ;
                _brand.addAttribute("brandenname", mobileBrand) ;
                _brand.addAttribute("brandcnname", mobileName) ;
                _brand.addAttribute("downloadroot", "http://yhzq.cn/front/download/downMobile.jsp?") ;
               
                Element _type = _brand.addElement("brandtype");
                _type.setText(str);
                
                brandId ++ ;

			}
			
			writer = new XMLWriter(new FileWriter(file), format);
            writer.write(_document);
            writer.close();
			
			System.out.println("finished");

		} catch (Exception e) {

			e.printStackTrace() ;
			
		} finally {
			try {
				if (newrs != null)
					newrs.close();
				if (newst != null)
					newst.close();
				if (newrs2!= null)
					newrs2.close();
				if (newst2 != null)
					newst2.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	
}
